package com.jkoss.wine.merchant.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.SqlHelper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jkoss.common.util.UuidUtil;
import com.jkoss.wine.merchant.entity.MerchantProductType;
import com.jkoss.wine.merchant.mapper.MerchantProductTypeMapper;
import com.jkoss.wine.merchant.service.IMerchantProductTypeService;
import com.jkoss.wine.shop.entity.ProductType;
import com.jkoss.wine.shop.entity.Shop;
import com.jkoss.wine.shop.mapper.ProductTypeMapper;
import com.jkoss.wine.shop.mapper.ShopMapper;

/**
 * 商品分类 服务实现类
 * 
 * @Author 周华军
 * @Version 1.0, 2018-09-26
 * @See
 * @Since com.jkoss.wine.merchant.service.impl
 * @Description: TODO
 */
@Service
public class MerchantProductTypeServiceImpl extends ServiceImpl<MerchantProductTypeMapper, MerchantProductType> implements IMerchantProductTypeService {

	@Autowired
	private MerchantProductTypeMapper merchantProductTypeMapper;
	@Autowired
	private ShopMapper shopMapper;
	@Autowired
	private ProductTypeMapper productTypeMapper;

	@Override
	public List<MerchantProductType> selectByTypeId() {
		// TODO Auto-generated method stub
		return this.baseMapper.selectByTypeId();
	}

	public boolean insertProductType(MerchantProductType merchantProductType) throws Exception {

		merchantProductTypeMapper.insert(merchantProductType);

		List<Shop> shops = shopMapper.selectList(null);
		List<ProductType> productTypes = new ArrayList<>();
		for (Shop shop : shops) {
			ProductType productType = new ProductType();
			BeanUtils.copyProperties(merchantProductType, productType);
			productType.setParentId(merchantProductType.getMerchantProductTypeId());
			productType.setProductTypeId(UuidUtil.getUUID());
			productType.setShopId(shop.getShopId());
			productType.setDel(0);
			productTypes.add(productType);
		}
		if (productTypes.size() > 0) {
			if (!retBool(productTypeMapper.insertBatch(productTypes))) {
				throw new Exception("添加商品类型失败");
			}
		}
		return true;
	}

	@Override
	public Integer getCount(String productTypeName, String merchantProductTypeId) {

		Wrapper wrapper = Condition.create().ne("del", 0).like("merchant_product_type_id", merchantProductTypeId).like("product_type_name", productTypeName);
		if (merchantProductTypeId != null) {
			wrapper.like("merchant_product_type_id", merchantProductTypeId);
		}
		wrapper.like("product_type_name", productTypeName);
		wrapper.ne("del", 0);
		return merchantProductTypeMapper.selectCount(wrapper);
	}

	@Override
	public boolean deleteProductType(String[] id) throws Exception {

		for (String string : id) {
			MerchantProductType merchantProductType = new MerchantProductType();
			merchantProductType.setMerchantProductTypeId(string);
			// 是否删除：0-未删除、1-已删除、2-不能删除
			merchantProductType.setDel(1);
			baseMapper.updateById(merchantProductType);
		}

		for (String string : id) {
			ProductType productType = new ProductType();
			productType.setParentId(string);
			// 是否删除：0-未删除、1-已删除、2-不能删除
			productType.setDel(1);
			Wrapper wrapper = Condition.create().eq("parent_id", string);
			productTypeMapper.update(productType, wrapper);
		}
		return true;
	}

	@Override
	public boolean updateProductType(MerchantProductType productType) throws Exception {

		int i = merchantProductTypeMapper.updateAllColumnById(productType);
		if (i != 1) {
			throw new Exception("更新商品类型失败:找不到该条记录");
		}
		ProductType productType2 = new ProductType();
		BeanUtils.copyProperties(productType, productType2);
		productType2.setParentId(productType.getMerchantProductTypeId());
		Wrapper wrapper = Condition.create().eq("parent_id", productType.getMerchantProductTypeId());
		if (!retBool(productTypeMapper.update(productType2, wrapper))) {
			throw new Exception("更新商品类型失败:找不到该条记录");
		}
		return true;
	}

	@Override
	public Page selectVoByPage(Page page, Wrapper wrapper) {
		SqlHelper.fillWrapper(page, wrapper);
		page.setRecords(baseMapper.selectVoByPage(page, wrapper));
		return page;
	}
}
